<!DOCTYPE html>
<html>
<head>
<title>本地文件上传</title>
#parse("sys/header.html")
<link type="text/css" rel="stylesheet" href="${rc.contextPath}/statics/plugins/fileinput/css/fileinput.css" />
<script type="text/javascript" src="${rc.contextPath}/statics/plugins/fileinput/js/fileinput.js"></script>
<script type="text/javascript" src="${rc.contextPath}/statics/plugins/fileinput/js/locales/zh.js"></script>
</head>
<body>
<div id="rrapp" v-cloak>
	<!-- <div class="container kv-main">
			<form enctype="multipart/form-data" action='../sysfileupload/imageUpload'>
		        <input id="file-0a" class="file-loading" type="file" name="imageFile" >multiple data-min-file-count="1"
		        <input type="text" class="form-control" name="dirName" value="text">
		        <button type="submit" class="btn btn-primary">Submit</button>
		        <button type="reset" class="btn btn-default">Reset</button>
		    </form>
	</div> -->
		
	<div v-show="showList">
		<div class="row">
			<!--#if($shiro.hasPermission("sysfileupload:save"))
			<a class="btn btn-primary" @click="add"><i class="fa fa-plus"></i>&nbsp;新增</a>
			#end
			#if($shiro.hasPermission("sysfileupload:update"))
			<a class="btn btn-primary" @click="update"><i class="fa fa-pencil-square-o"></i>&nbsp;修改</a>
			#end -->
			<div class="form-group col-sm-6">
					<input id="file-0a" class="file-loading" type="file" name="imageFile" >
			</div>
			#if($shiro.hasPermission("sysfileupload:delete"))
			<a class="btn btn-primary" @click="del"><i class="fa fa-trash-o"></i>&nbsp;删除</a>
			#end
			
		</div>
	    <table id="jqGrid"></table>
	    <div id="jqGridPager"></div>
    </div>
    
    <div v-show="!showList" class="panel panel-default">
		<div class="panel-heading">{{title}}</div>
		<form class="form-horizontal">
			<div class="form-group">
			   	<div class="col-sm-2 control-label">URL地址</div>
			   	<div class="col-sm-10">
			      <input type="text" class="form-control" v-model="sysFileupload.url" placeholder="URL地址"/>
			    </div>
			</div>
			<div class="form-group">
				<div class="col-sm-2 control-label"></div> 
				<input type="button" class="btn btn-primary" @click="saveOrUpdate" value="确定"/>
				&nbsp;&nbsp;<input type="button" class="btn btn-warning" @click="reload" value="返回"/>
			</div>
		</form>
	</div>
</div>

<!-- <script src="${rc.contextPath}/js/generator/sysfileupload.js?_${date.systemTime}"></script> -->
<script type="text/javascript">
$(function () {
    $("#jqGrid").jqGrid({
        url: '../sysfileupload/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: 'URL地址', name: 'url', index: 'url', width: 100 },
			{ label: '图片', name: 'url', width: 20 ,sortable: false ,align: "center",formatter: function(value, options, row){
				return value == null ? '' : '<img src="'+value+'" alt="" width="50px"/>';
			}},
			{ label: '创建时间', name: 'createDate', index: 'create_date', width: 50 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		sysFileupload: {}
	},
	created: function() {
		$(document).on('ready', function() {
			$("#file-0a").fileinput({
				language: 'zh'//设置语言
				,showUpload: true //是否显示上传按钮
		        ,showCaption: true //是否显示标题
		        ,dropZoneEnabled: false //是否显示拖拽区域
				,uploadUrl: '../sysfileupload/imageUpload'//上传的地址
				,maxFileSize: 1000
				,maxFileCount:1 //表示允许同时上传的最大文件个数
				//,allowedFileTypes: ['image', 'video', 'flash']//文件类型
				,uploadExtraData:{"id": 1, "dirName":'text'}
				,allowedFileExtensions: ['jpg', 'gif', 'png']//接收的文件后缀
				,slugCallback: function (filename) {
		            return filename.replace('(', '_').replace(']', '_');
		        }
			}).on("fileuploaded", function (event, data, previewId, index) {
				console.log(data.response);
				vm.reload();
			});
			$('#file-0a').on('fileerror', function(event, data, msg) {
				console.log(data.response);
			});
		});
    },
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.sysFileupload = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.sysFileupload.id == null ? "../sysfileupload/save" : "../sysfileupload/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.sysFileupload),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../sysfileupload/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../sysfileupload/info/"+id, function(r){
                vm.sysFileupload = r.sysFileupload;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});
</script>
</body>
</html>